/* eslint-disable no-unused-vars */
import namespace from "./namespace";
import extend from "lodash/extend";

/**
 * 放大 缩小 角标类
 */
class ResizeHandle extends namespace.shape.Rectangle {
  NAME = `${namespace.namespace}.ResizeHandle`;

  static NORTH_WEST = 1; //西北 左上角
  static NORTH_EAST = 2; //东北 右上角
  static SOUTH_EAST = 3; //东南 左下角
  static SOUTH_WEST = 4; //东西 右下角

  /**
   * owner 角标的所属图形
   */
  constructor(attr) {
    super();

    // 当前图形为放大缩小图形
    this.isHandleFigures = true;

    this.setterWhitelist = extend(this.setterWhitelist, {
      owner(owner) {
        return this.setOwner(owner);
      },
      type(type) {
        return this.setType(type);
      }
    });

    super.attr(
      extend(
        {
          bgColor: "#0087ff",
          strokeWidth: 0.5,
          width: 8,
          height: 8,
          radius: 0,
          selectable: false
        },
        attr
      )
    );
  }

  createShapeElement() {
    const shape = super.createShapeElement();
    this.updateCursor(shape);
    return shape;
  }

  updateCursor(shape) {
    if (shape === null) {
      return this;
    }

    if (this.isDraggable() === false) {
      shape.attr({ cursor: "default" });
      return this;
    }

    switch (this.type) {
      case 1:
        shape.attr({ cursor: "nw-resize" });
        break;
      case 2:
        shape.attr({ cursor: "ne-resize" });
        break;
      case 3:
        shape.attr({ cursor: "se-resize" });
        break;
      case 4:
        shape.attr({ cursor: "sw-resize" });
        break;
      default:
        shape.attr({ cursor: "move" });
        break;
    }
    return this;
  }

  setOwner(owner) {
    this.owner = owner;
  }

  setType(type) {
    this.type = type;
  }

  show(canvas) {
    this.setCanvas(canvas);
    this.canvas.handleFigures.add(this);
    this.shape.attr("type", this.type);
    this.shape.insertAfter(this.owner.getShapeElement());
    this.repaint();

    return this;
  }

  hide() {
    this.canvas.handleFigures.remove(this);
    this.setCanvas(null);
    return this;
  }

  setPosition(x, y) {
    if (x instanceof namespace.geo.Point) {
      this.x = x.x;
      this.y = x.y;
    } else {
      this.x = x;
      this.y = y;
    }

    if (this.repaintBlocked === true || this.shape === null) {
      return this;
    }

    this.shape.attr({ x: this.x, y: this.y });

    this.applyTransformation();
  }

  onDragStart(x, y, shiftKey, ctrlKey) {
    if (!this.isDraggable()) {
      return false;
    }

    this.ox = this.getAbsoluteX();
    this.oy = this.getAbsoluteY();
  }

  onDrag(dx, dy, dx2, dy2) {
    if (this.isDraggable() === false) {
      return;
    }

    let oldX = this.getAbsoluteX();
    let oldY = this.getAbsoluteY();

    // 父类更新坐标 宽高 以及角标位置
    super.onDrag(dx, dy, dx2, dy2);

    let diffX = this.getAbsoluteX() - oldX;
    let diffY = this.getAbsoluteY() - oldY;

    let obj = this.owner;
    let objPosX = obj.getAbsoluteX();
    let objPosY = obj.getAbsoluteY();
    let objWidth = obj.getWidth();
    let objHeight = obj.getHeight();

    let newX = null;
    let newY = null;
    let corrPos = null;

    switch (this.type) {
      case ResizeHandle.NORTH_WEST:
        obj.setDimension(objWidth - diffX, objHeight - diffY);
        newX = objPosX + (objWidth - obj.getWidth());
        newY = objPosY + (objHeight - obj.getHeight());
        obj.setPosition(newX, newY);
        break;
      case ResizeHandle.NORTH_EAST:
        obj.setDimension(objWidth + diffX, objHeight - diffY);
        newX = objPosX;
        newY = objPosY + (objHeight - obj.getHeight());
        obj.setPosition(newX, newY);
        break;
      case ResizeHandle.SOUTH_EAST:
        obj.setDimension(objWidth + diffX, objHeight + diffY);
        break;
      case ResizeHandle.SOUTH_WEST:
        obj.setDimension(objWidth - diffX, objHeight + diffY);
        newX = objPosX + (objWidth - obj.getWidth());
        newY = objPosY;
        obj.setPosition(newX, newY);
        break;
    }

    if (newX !== null) {
      corrPos = obj.getPosition();
      if (corrPos.x !== newX || corrPos.y !== newY) {
        obj.setDimension(
          obj.getWidth() - (corrPos.x - newX),
          obj.getHeight() - (corrPos.y - newY)
        );
      }
    }
  }

  onDragEnd(x, y, shiftKey, ctrlKey) { }
}

namespace.ResizeHandle = ResizeHandle;
